* Macros to computes Census tract characteristics using 2000 Census SF3;
* Produced by Mark Kutzbach;

* macro to be run in primary program;
%macro tract_census;

* geo is tract, set up for 2000 Census, but could add other items;
/* run census 2000 macro  --------------------------------------------------------------- */
%if (&run_census2000.=1) %then %do;
/* create variables from census 2000 Data Ferrett data */
    * identify geo pairs;
    data census2000 (keep=h_stfid
                          pop2000 pop_sq_mile 
                          vacancy_rate owner_rate year_built_med 
                          com_veh_rate com_pub_rate veh_0_rate
                          poverty_rate assist_rate edu_hs_rate emp_rate
                    );
        length h_st $2 h_ct $3 h_tr $6 h_stfid $11
                          pop2000 pop_sq_mile 5.
                          vacancy_rate owner_rate year_built_med 
                          com_veh_rate com_pub_rate veh_0_rate
                          poverty_rate assist_rate edu_hs_rate emp_rate 3.;       
        set TRACTCEN.census2000_pu (where=(put(STATE,z2.)="&stcode."));
        h_st=put(STATE,z2.);
        h_ct=put(COUNTY,z3.);
        h_tr=put(TRACT,z6.);
        h_stfid=h_st||h_ct||h_tr;
        * remove zero or low population tracts;
        if (not ((h_tr='000000') or (P001001<10) or (H001001<10) or 
            (P030001=0) or (H006002=0) or (P037002=0 and P037019=0)));
        sample_tract=0;
        * format tract variables;
        pop2000=P001001;
        pop_sq_mile=P001001/AREALAND;
        vacancy_rate=H006003/H001001;
        owner_rate=H007002/H006002;
        com_veh_rate=P030002/P030001;
        com_pub_rate=P030005/P030001;
        veh_0_rate=(H044003+H044010)/H006002;
        year_built_med=H035001-1900; * 1939 to 1999;
        poverty_rate=P087002/P001001;
        edu_hs_rate=(P037011+P037012+P037013+P037014+P037015+P037016+P037017+P037018+
                     P037028+P037029+P037030+P037031+P037032+P037033+P037034+P037035)/
                    (P037002+P037019);
        emp_rate=(P043006+P043013)/P043001;
        assist_rate=P064002/P001001;
    run;
    * sort for next steps;
    proc sort data=census2000 out=OUTDATA.census2000_&met.;
        by h_stfid;
    run;
    proc contents data=OUTDATA.census2000_&met.;
    run;
%end; * end macro text;

%mend tract_census; * end census 2000 macro;
